package name.fowler.refactoring.simplestore;

import java.util.ArrayList;
import java.util.List;

public class Customer {

  private String m_Name;
  private List<Rental> m_Rentals = new ArrayList<Rental>();
  private double totalAmount;
  private int frequentRenterPoints;

  public Customer(String name) {
    m_Name = name;
  }

  public int getM_RentalsSize() {
    return m_Rentals == null ? 0 : m_Rentals.size();
  }

  public String getName() {
    return m_Name;

  }

  public void addRental(Rental arg) {
    m_Rentals.add(arg);
  }

  public String statement() {
    totalAmount = 0;
    frequentRenterPoints = 0;

    StringBuilder result = new StringBuilder("Rental record for " + m_Name + "\n");

    for (Rental each : m_Rentals) {
      double thisAmount = 0;

      totalAmount += each.getMovie().calcAmount(each.getDaysRented());

      // Add frequent renter points
      frequentRenterPoints++;

      // Add bonus for a two-day new-release rental
      if ((each.getMovie() instanceof MovieNewRelease) && (each.getDaysRented() > 1)) {
        frequentRenterPoints++;
      }

      // Show figures for this rental
      result.append("\t" + each.getMovie().getTitle() + "\t" + thisAmount + "\n");
      totalAmount += thisAmount;
    }

    // Add footer lines
    result.append("Amount owed is " + totalAmount + "\n");
    result.append("You earned ").append(frequentRenterPoints).append(" frequent renter points.");
    return result.toString();
  }

  public double getTotalAmount() {
    return totalAmount;
  }

  public int getFrequentRenterPoints() {
    return frequentRenterPoints;
  }
}

